package gui;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import objects.AlarmContext;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * Practica 1, Ingenieria del Software II Universidad de Cantabria
 * 
 * 
 */
public class AlarmClockGUI {

	protected Shell shell;
	private Button snooze;
	private Button buzz;
	private Button stop;
	private Button alarmaOn;
	private Button alarmaOff;
	private DateTime horaAlarma;
	private Combo snoozeTime;

	private AlarmContext alarmClock = new AlarmContext();

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			AlarmClockGUI window = new AlarmClockGUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(452, 237);
		shell.setText("Despertador");

		snoozeTime = new Combo(shell, SWT.READ_ONLY);
		snoozeTime.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO
			}
		});

		final String ONE_MIN = "1 min";
		final String FIVE_MINS = "5 min";
		final String TEN_MINS = "10 min";
		final String FIFTEEN_MINS = "15 min";

		snoozeTime.setItems(new String[] { ONE_MIN, FIVE_MINS, TEN_MINS,
				FIFTEEN_MINS });
		snoozeTime.setBounds(322, 36, 91, 23);

		Label lblSnoozeTime = new Label(shell, SWT.NONE);
		lblSnoozeTime.setBounds(321, 15, 75, 15);
		lblSnoozeTime.setText("Snooze Time");

		snooze = new Button(shell, SWT.NONE);
		snooze.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				/////////////////////
				// XXX TEMP MAP ! I promise to update to Java7 soon... (Sergio)
				Map<String, Integer> tabla = new HashMap<String, Integer>();
				tabla.put(ONE_MIN, 1);
				tabla.put(FIVE_MINS, 2);
				tabla.put(TEN_MINS, 3);
				tabla.put(FIFTEEN_MINS, 4);
				
//				switch (snoozeTime.getText()) {
				switch (tabla.get(snoozeTime.getText())) {
				case 1:	//ONE_MIN:
					alarmClock.snooze(1);
					break;
				case 2:	//FIVE_MINS:
					alarmClock.snooze(5);
					break;
				case 3:	//TEN_MINS:
					alarmClock.snooze(10);
					break;
				case 4:	//FIFTEEN_MINS:
					alarmClock.snooze(15);
					break;
				default:
					alarmClock.snooze(1);
					break;
				}
				/////////////////////
			}
		});
		snooze.setBounds(332, 65, 75, 33);
		snooze.setText("Snooze");

		buzz = new Button(shell, SWT.TOGGLE);
		buzz.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				alarmClock.setBuzz();
			}
		});
		buzz.setBounds(208, 30, 75, 33);
		buzz.setText("Buzz");

		stop = new Button(shell, SWT.NONE);
		stop.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				alarmClock.stop();
			}
		});
		stop.setBounds(208, 119, 75, 33);
		stop.setText("Stop");

		horaAlarma = new DateTime(shell, SWT.BORDER | SWT.TIME);
		horaAlarma.setBounds(84, 89, 75, 24);

		Label lblHoraalarma = new Label(shell, SWT.NONE);
		lblHoraalarma.setBounds(10, 89, 89, 24);
		lblHoraalarma.setText("HoraAlarma");

		alarmaOn = new Button(shell, SWT.NONE);
		alarmaOn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Calendar time = Calendar.getInstance();
				time.set(Calendar.HOUR_OF_DAY, horaAlarma.getHours());
				time.set(Calendar.MINUTE, horaAlarma.getMinutes());
				time.set(Calendar.SECOND, horaAlarma.getSeconds());
				alarmClock.alarmOn(time);

			}
		});
		alarmaOn.setBounds(43, 34, 98, 25);
		alarmaOn.setText("AlarmaOn");

		alarmaOff = new Button(shell, SWT.NONE);
		alarmaOff.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO
			}
		});
		alarmaOff.setBounds(43, 149, 96, 25);
		alarmaOff.setText("AlarmaOff");
	}
}
